package com.imooc.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.imooc.pojo.R;
import com.imooc.pojo.Resource;
import com.imooc.pojo.User;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	Resource resource;
	

	@RequestMapping("/hello")
	public String hello(){
		return "hello ~~~";
	}
	
	@RequestMapping("resource")
	public R getResource(){
		Resource bean = new Resource();
		
		BeanUtils.copyProperties(resource, bean);
		return R.ok(bean);
	}
	
	@RequestMapping("/get")
	public R getUser(@RequestParam String birth, @RequestParam(required=false) String desc) throws ParseException{
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		User user = new User();
		try {
			user.setName("王木子");
			user.setAge(20);
			user.setDesc(desc);
			user.setPassword("123456");
			user.setBirthday(sdf.parse(birth));
		} catch (Exception e) {
			e.printStackTrace();
			return R.errorMsg("生日birth不能为空");
		}
		
		return R.ok(user);
	}
	
}
